﻿//This filter is tested and seems to work correctly
//TODO: ConservativeSmoothing: Test if this is correctly impelmented

using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Windows.Forms;

namespace LowLevelGraphics.Filter
{
    /// <summary>
    /// Color intensity
    /// This is used by conservative smoothing
    /// </summary>
    public struct ColorIntensity : IComparer<ColorIntensity>
    {
        public Color color;
        public int Intensity;

        /// <summary>
        /// primary constructor to specify color and intensity directly
        /// </summary>
        /// <param name="_color"></param>
        /// <param name="_nIntensity"></param>
        public ColorIntensity(Color _color, int _nIntensity)
        {
            color = _color;
            Intensity = _nIntensity;
        }

        /// <summary>
        /// default icomparer to compare two colors intensities
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <returns></returns>
        public int Compare(ColorIntensity x, ColorIntensity y)
        {
            if (x.Intensity > y.Intensity)
            {
                return 1;
            }
            else if (x.Intensity < y.Intensity)
            {
                return -1;
            }
            else
            {
                return 0;
            }
        }
    }

    /// <summary>
    /// Smoothes an image in a onservative way i.e. 
    /// if the value is is not contained in the given intervall it is set to one of the boundaries of the intervall
    /// (min/max)
    /// </summary>
    public class ConservativeSmoothing : BaseImageFilter
    {
        /// <summary>
        /// default constructor
        /// </summary>
        public ConservativeSmoothing()
        {
        }

        /// <summary>
        /// used by clone to create a deep copy
        /// </summary>
        /// <param name="_conservativeSmoothing"></param>
        internal ConservativeSmoothing(ConservativeSmoothing _conservativeSmoothing)
        {
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="_bitmap"></param>
        public override UnsafeBitmap Execute(UnsafeBitmap _bitmap)
        {
            UnsafeBitmap bitmap = _bitmap;

            List<Point> aPoint = Definitions.EnvironmentPointListClockwise;

            Color[] aColor = new Color[8];
            List<ColorIntensity> aIntensity = new List<ColorIntensity>();

            Color color = Color.Transparent;
            Color colorToSet = Color.Transparent;

            int nWidth = bitmap.Width;
            int nHeight = bitmap.Height;

            int y = 0;
            int x = 0;

            for (y = 0; y < nHeight; y++)
            {
                for (x = 0; x < nWidth; x++)
                {
                    color = bitmap.GetPixel(x, y);
                    int nIntensity = GetIntensity(color);
                    aColor = Segmentation.GetEnvironmentPixelColorArray(bitmap, x, y, aPoint, aColor);

                    aIntensity.Clear();
                    for (int i = 0; i < aColor.Length; i++)
                    {
                        aIntensity.Add(new ColorIntensity(aColor[i], GetIntensity(aColor[i])));
                    }
                    aIntensity.Sort(new ColorIntensity());
                    if (nIntensity < aIntensity[0].Intensity)
                    {
                        bitmap.SetPixel(x, y, aIntensity[0].color);
                    }
                    else if (nIntensity > aIntensity[7].Intensity)
                    {
                        bitmap.SetPixel(x, y, aIntensity[7].color);
                    }
                }
            }
            return _bitmap;
        }

        /// <summary>
        /// Get intensity of a color
        /// </summary>
        /// <param name="_color"></param>
        /// <returns></returns>
        private int GetIntensity(Color _color)
        {
            return (int)(_color.GetBrightness() * 255.0f);
        }

        /// <summary>
        /// used by clone to create a deep copy
        /// </summary>
        /// <returns></returns>
        public override object Clone()
        {
            return new ConservativeSmoothing(this);
        }
    }
}
